package cn.tedu.live8.controller;

import cn.tedu.live8.mapper.LoginAndRegMapper;
import cn.tedu.live8.pojo.entity.VipUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
public class RegAndLoginController implements UserDetailsService {
    @Autowired
    private LoginAndRegMapper mapper;

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private AuthenticationManager authenticationManager;


    @RequestMapping("/VipLogin/reg")
    public String reg(@RequestBody VipUser user){
        String rawPassword = user.getPassword();
        String encode = passwordEncoder.encode(rawPassword);
        user.setPassword(encode);
        log.debug("开始处理注册");
        mapper.reg(user);
        return "注册成功";
    }
    @RequestMapping("/VipLogin/login")
    public int login(@RequestBody VipUser vipUser, HttpSession session) throws AuthenticationException {
        log.debug("开始处理登录:"+vipUser);
        UserDetails uuu = loadUserByUsername(vipUser.getUsername());
        log.debug("UserDetails:{}",uuu);

        if(uuu==null){
            log.debug("密码错误!!!!!!");
            return 0;
        }if(uuu!=null){
            log.debug("通过!!!!!!!!!!!!!");
            session.setAttribute("user",vipUser);
            log.debug("session.:{}",session.getAttribute("user"));
        }

//        Authentication authentication =
//                new UsernamePasswordAuthenticationToken(vipUser.getUsername(),vipUser.getPassword());
//
//        log.debug("八八八八八Abba爸爸");
//          authenticationManager.authenticate(authentication);
        return 1;
    }
    @RequestMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("user");
    }

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        log.debug("Spring Security自动根据用户名【{}】查询用户详情", s);
        VipUser vipUser = mapper.login(s);
        // 以下是临时使用的代码
        if(vipUser !=null){
            UserDetails userDetails = User.builder()
                    .username(vipUser.getUsername())
                    .password(vipUser.getPassword())
                    .disabled(false)
                    .accountLocked(false) // 账号是否锁定
                    .accountExpired(false) // 账号是否过期
                    .credentialsExpired(false) // 认证是否过期
                    .authorities("临时给出任意字符串") // 【必须】此账号的权限信息
                    .build();
            return userDetails;
        }
        return null;
    }


}
